package cn.itsource.org.service.impl;

import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service//加载当前service类
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)//添加事务，只读，没有事务就以非事务执行
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> findAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Transactional//增删改需要事务
    @Override
    public void add(Department department) {
        //先新增，获取到主键id
        departmentMapper.save(department);
        //1.生成当前/自己的id  地址
        String myDir = "/"+department.getId();
        //判断当前部门对象，是否有上级部门
        if(department.getParent_id()!=null){//有上级部门
            //生成新的部门地址：上级部门地址+/自己id
            //父级部门对象
            Department parentDept = departmentMapper.loadById(department.getParent_id());
            myDir = parentDept.getDirPath()+myDir;
        }
        //部门地址之前为空，给部门地址赋值
        department.setDirPath(myDir);
        //修改部门地址
        departmentMapper.update(department);
    }

    @Transactional//增删改需要事务
    @Override
    public void delById(Long id) {
        departmentMapper.remove(id);
    }

    @Transactional//增删改需要事务
    @Override
    public void update(Department department) {
        //1.生成当前/自己的id  地址
        String myDir = "/"+department.getId();
        //判断当前部门对象，是否有上级部门
        if(department.getParent_id()!=null){//有上级部门
            //生成新的部门地址：上级部门地址+/自己id
            //父级部门对象
            Department parentDept = departmentMapper.loadById(department.getParent_id());
            myDir = parentDept.getDirPath()+myDir;
        }
        //部门地址之前为空，给部门地址赋值
        department.setDirPath(myDir);
        departmentMapper.update(department);
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery departmentQuery) {
        //查询总条数
        Integer count = departmentMapper.queryCount(departmentQuery);
        //总条数为0或者null，不需要分页查询了，直接返回结果
        if (count==null || count==0){
            return  new PageList<Department>();
        }
        //查询当前页数据
        List<Department> list = departmentMapper.queryData(departmentQuery);
        //调用全参构造
        return new PageList<Department>(count,list);
    }

    @Override
    public void parchDel(List<Long> ids) {
        departmentMapper.parchDel(ids);
    }

    //实现查询部门树
    @Override
    public List<Department> findDeptTree() {
        //1.查询到所有部门
        List<Department> ds = departmentMapper.loadAll();
        //2.把所有部门的list转换为map,key就是部门的id，value就是部门对象自己
        Map<Long, Department> map = ds.stream().collect(
                Collectors.toMap(Department::getId, (d) -> d)
        );
        //3.定义返回的部门树集合
        List<Department> deptTree = new ArrayList<Department>();
        //4.给部门树集合赋值
        for (Department d:ds){
            if(d.getParent_id()==null){//装入1级部门，1级部门没有上级部门id
                deptTree.add(d);
            }else { //就不是1级部门，那么要把自己装入上级部门的 子部门list中
                //找到上级部门
                Department parentDept = map.get(d.getParent_id());
                //装入上级部门的子部门list
                parentDept.getChildren().add(d);
            }
        }
        return deptTree;
    }
}
